﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Main;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using SenseTimeTracker.Reports;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Projects
{
	public class ProjectsSliderPanel : CommonSliderPanel<Project>
	{
		public ProjectsSliderPanel(SenseTimeTrackerForm form)
			: base(form, "ProjectSmallIcon.png", "ProjectLargeIcon.png", MenuEnty.All)
		{
			sorting = Persistance.Instance.ProjectsSorting;
			sortColumns = SortColumn.Name | SortColumn.Type | SortColumn.Number;
		}

		public override string Name { get { return Resources.PanelProjectsName; } }

		protected override void MenuItemNewEntry_Click(object sender, EventArgs e)
		{
			using (new DialogHelper(ownedForm))
			{
				ProjectEditForm form = new ProjectEditForm
				{
					Project = new Project { Client = Persistance.Instance.FilterForProjects.ClientFilter },
				};
				if (form.ShowDialog() == DialogResult.OK)
				{
					form.Project.Insert();
					Bind(senseListControl);
				}
			}
		}

		protected override void MenuItemEditEntry_Click(object sender, EventArgs e)
		{
			ProjectDisplayItem item = senseListControl.FocusedItem as ProjectDisplayItem;
			if (item != null)
			{
				EditProject(item.Project.Id);
				Bind(senseListControl);
			}
		}

		protected override void MenuItemCopyEntry_Click(object sender, EventArgs e)
		{
			CopyAndEditProject(false);
		}

		protected override void MenuItemDuplicateEntry_Click(object sender, EventArgs e)
		{
			CopyAndEditProject(true);
		}

		protected override void MenuItemRemoveEntry_Click(object sender, EventArgs e)
		{
			if (SenseAPIs.SenseMessageBox.Show(Resources.DialogDeleteQuestion, Resources.DialogHeaderRemoveProject, SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				ProjectDisplayItem item = senseListControl.FocusedItem as ProjectDisplayItem;
				if (item != null)
				{
					Project.Delete(item.Project.Id);
					Bind(senseListControl);
				}
			}
		}

		public override void Bind(SenseListControl senseListControl)
		{
			base.Bind(senseListControl);
			filterItem.PrimaryText = string.Format(
        "{0}: {1}",
				Resources.ClientLabel,
				DatabaseManager.ClientName(Persistance.Instance.FilterForProjects.ClientFilter));

			senseListControl.AddItem(new SensePanelDividerItem(string.Empty, Resources.ProjectsExplanation));

			var filteredProjects = from project in Project.Projects select project;
			if (Persistance.Instance.FilterForProjects.ClientFilter != DatabaseManager.AllClientsId)
			{
				filteredProjects = from project in filteredProjects where project.Client == Persistance.Instance.FilterForProjects.ClientFilter select project;
			}

			SortFilter(ref filteredProjects);
			foreach (Project project in filteredProjects)
			{
				senseListControl.AddItem(new ProjectDisplayItem
				{
					Project = project,
				});
			}
		}

		public override void BindFilter()
		{
			SensePanelComboItem clientFilterComboBox = new SensePanelComboItem();
			clientFilterComboBox.LabelText = string.Format("{0}:", Resources.ClientLabel);
			clientFilterComboBox.ShowSeparator = true;
			Binder.BindCombobox(clientFilterComboBox.Items, Guid.Empty, Guid.Empty, true, false);
			clientFilterComboBox.SelectedValue = Persistance.Instance.FilterForProjects.ClientFilter;
			clientFilterComboBox.OnSelectedValueChanged += clientFilterComboBox_OnSelectedValueChanged;
			if (clientFilterComboBox.SelectedIndex == -1)
			{
				clientFilterComboBox.SelectedIndex = 0;
				Persistance.Instance.FilterForProjects.ClientFilter = (Guid)clientFilterComboBox.Items[0].Value;
			}

			BindFilter(clientFilterComboBox, Resources.ProjectsFilterHeader);
		}

		private void clientFilterComboBox_OnSelectedValueChanged(object Sender, object Value)
		{
			Persistance.Instance.FilterForProjects.ClientFilter = (Guid)Value;
		}

		/// <summary>
		/// Edit projets
		/// </summary>
		/// <param name="projectId">Id of the project to be edited</param>
		/// <returns>True, if the edit was succesfil</returns>
		private DialogResult EditProject(Guid projectId)
		{
			DialogResult dialogResult = DialogResult.Cancel;

			using (new DialogHelper(ownedForm))
			{
				ProjectEditForm form = new ProjectEditForm
				{
					Project = Project.GetProject(projectId),
				};
				if (form.ShowDialog() == DialogResult.OK)
				{
					form.Project.Update();
					dialogResult = DialogResult.OK;
				}
			}

			return dialogResult;
		}

		/// <summary>
		/// Copies and edits the project
		/// </summary>
		/// <param name="doubleProject">Indicates whether the project shall be doubled</param>
		private void CopyAndEditProject(bool doubleProject)
		{
			ProjectDisplayItem item = senseListControl.FocusedItem as ProjectDisplayItem;
			Guid oldProjectId = item.Project.Id;
			Guid newProjectId = Project.Copy(oldProjectId, null);
			if (EditProject(newProjectId) == DialogResult.OK)
			{
				if (doubleProject)
				{
					foreach (Guid taskId in DatabaseManager.ProjectAllTasks(oldProjectId))
					{
						DatabaseManager.TaskCopy(taskId, newProjectId);
					}
				}
			}
			else
			{
				Project.Delete(newProjectId);
			}

			Bind(senseListControl);
		}
	}
}
